<?php
/**
 * Created by PhpStorm.
 * User: xiaoxin
 * Date: 2018/10/25 0025
 * Time: 15:42
 */
include './functions.php';
include './Db.php';
$post = $_POST;
session_start();
if ($post['captcha']!=$_SESSION['code']){
	exit(json_encode(['code'=>1,'msg'=>'验证码错误']));
}
$preg_phone = "/^1[0-9]{10}$/";
$yes2 = preg_match($preg_phone,$post['phone']);
if (!$yes2){
	exit(json_encode(['code'=>1,'msg'=>'手机格式不符合要求']));
}
$preg_password = "/^[\w@]{6,20}$/";
$yes3 = preg_match($preg_password,$post['password']);
if (!$yes3){
	exit(json_encode(['code'=>1,'msg'=>'密码不符合要求']));
}
if ($post['password']!=$post['repassword']){
	exit(json_encode(['code'=>1,'msg'=>'两次密码不一致']));
}
$db = new Db('user');
$db->where('phone=\''.$post['phone']);
$res = $db->select();
$db->where('password=\''.md5($post['password']));
$ress = $db->select();
if ($res && $ress){
	$_SESSION['user_id']=$res[0]['user_id'];
	$_SESSION['username']=$res[0]['username'];
	exit(json_encode(['code'=>0,'msg'=>'登陆成功']));
}else{
	exit(json_encode(['code'=>1,'msg'=>'没有找到该用户']));
}
